<template>
	<!--	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">-->
	<!--		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">-->
	<!--			<a-row :gutter="24">-->
	<!--				<a-col :span="8">-->
	<!--					<a-form-item label="名称关键词" name="searchKey">-->
	<!--						<a-input v-model:value="searchFormState.searchKey" placeholder="请输入模块名称关键词" />-->
	<!--					</a-form-item>-->
	<!--				</a-col>-->
	<!--				<a-col :span="8">-->
	<!--					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>-->
	<!--					<a-button class="xn-mg08" @click="reset">重置</a-button>-->
	<!--				</a-col>-->
	<!--			</a-row>-->
	<!--		</a-form>-->
	<!--	</a-card>-->
	<a-card :bordered="false">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:scroll="{ x: 1500, y: 500 }"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen(false, total)">
						<template #icon><plus-outlined /></template>
						新增用户
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'create_time'">
					{{ dayjs(record.create_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'modified_time'">
					{{ dayjs(record.modified_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'start_time'">
					{{ dayjs(record.start_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'end_time'">
					{{ dayjs(record.end_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag :color="{ 1: 'processing', 2: 'success', 3: 'error' }[record.status]">{{
						{ 1: '创建中', 2: '生效', 3: '过期' }[record.status]
					}}</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)">编辑</a>
						<a-divider type="vertical" />
						<a-popconfirm title="确定要删除此客户吗？" @confirm="deleteModule(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="orderList">
	import Form from './form.vue'
	import dayjs from 'dayjs'
	import api from './api'
	const searchFormState = ref({})
	const formRef = ref()
	const searchFormRef = ref()
	const tableRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '用户名称',
			dataIndex: 'username',
			align: 'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center'
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const total = ref(0)

	const loadData = (parameter) => {
		return api.modulePage(Object.assign(parameter, searchFormState.value)).then((res) => {
			total.value = res.total + 1
			return {
				current: parameter.pageNow,
				records: res.record,
				total: res.total
			}
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteModule = (record) => {
		let params = {
			id: record.id
		}

		api.moduleDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
</script>
